package com.zmj.demo.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.demo.pojo.entity.DeptEntity;
import com.zmj.demo.pojo.entity.req.UserReqDelDTO;
import com.zmj.framework.base.controller.BaseController;
import com.zmj.framework.request.PageRequest;
import com.zmj.framework.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zmj.demo.pojo.entity.FormEntity;
import com.zmj.demo.service.FormService;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mingguang
 * @since 2024-04-01
 */
@RestController
@Api(tags = { "FormEntity" })
@RequestMapping("/v1/api/formEntity")
public class FormController extends BaseController<FormService, FormEntity> {

    @ApiOperation(value = "※ form 分页查询")
    @Operation(summary = "※ form 分页查询", description = "form 分页查询")
    @PostMapping(value = "/pageList")
    public Result<IPage<FormEntity>> pageList(@RequestBody PageRequest request) {
        Map<String, Object> param = request.getParam() == null ? new HashMap<>() : request.getParam();
        param.put("order",request.getOrder());
        param.put("orderBy",request.getOrderBy());
        IPage<FormEntity> page = new Page<>(request.getPageInfo().getPageIndex(), request.getPageInfo().getPageSize());
        page = baseService.selectPageList(page, param);
        return Result.success(page);
    }


    @ApiOperation(value = "※ 新增或编辑保存")
    @Operation(summary = "※ 新增或编辑保存", description = "新增或编辑保存")
    @PostMapping(value = "/saveOrUpdate")
    public Result<?> saveOrUpdate(@Valid @RequestBody FormEntity formEntity) {
        
        baseService.saveOrUpdate(formEntity);
        return Result.success();
    }

    @ApiOperation(value = "※ form 删除")
    @Operation(summary = "※ form 删除", description = "form 删除")
    @PostMapping(value = "/delete")
    public Result<String> deleteById(@RequestBody UserReqDelDTO userReqDelDTO) {
        Assert.notBlank(userReqDelDTO.getIds(),"ids不能为空！");
        List<String> idsStr = Arrays.asList(userReqDelDTO.getIds().split(","));
        List<Integer> ids = idsStr.stream().map(str -> Integer.valueOf(str)).collect(Collectors.toList());
        baseService.removeBatchByIds(ids);
        return Result.success();
    }


    @ApiOperation(value = "※ 详情")
    @GetMapping(value = "info/{id}")
    public Result<FormEntity> info(@PathVariable Integer id) {
        FormEntity formEntity = baseService.getById(id);
        return Result.success(formEntity);
    }


}
